/**
 * 获取当前的时间
 * @return {string} {格式化后的当前时间}
 */
export function getCurrentDate(mode) {
	const current_date = new Date();

	const year = current_date.getFullYear();

	let months = current_date.getMonth() + 1;

	let date = current_date.getDate();

	if (months < 10) {
		months = `0${months}`;
	}

	if (date < 10) {
		date = `0${date}`;
	}

	if (mode) {
		switch (mode) {
		case 'year':

			return year;
		case 'y-m':

			return `${year}-${months}`;

		case 'month':

			return months;

		case 'day':

			return date;
		default:
			return;
		}
	}

	return `${year}-${months}-${date}`;
}

/**
 * 设置页面为屏幕宽度
 * @param  {string} class_name {需要进行设置的元素的class}
 * @return {undefined}
 */
export function setScreenHeight(class_name) {
	const main_div = document.getElementsByClassName(class_name)[0];

	if (!main_div) return;

	main_div.style.minHeight = `${window.innerHeight}px`;
}

/**
 * 网址格式化
 * @return {Object} {格式化网址之后得到的参数对象}
 */
export function formaturl() {
	const url_info = {};

	const url_object = window.location.href.split('#').shift().split('?').pop();

	url_object.split('&').forEach((url) => {
		const key = url.split('=').shift();
		const value = url.split('=').pop();

		if (key) {
			url_info[key] = value;
		}
	});

	return url_info;
}

/**
 * 判断a数组是否包含b数组
 * @param  {array} a {判断的a数组}
 * @param  {array} b {判断的b数组}
 * @return {Boolean} {true代表包含，false代表不包含}
 */
export function isContained(a, b) {
	if (!(a instanceof Array) || !(b instanceof Array)) return false;

	if (a.length < b.length) return false;

	const aStr = a.toString();

	for (let i = 0, len = b.length; i < len; i += 1) {
		if (aStr.indexOf(b[i]) === -1) return false;
	}

	return true;
}

/**
 * 图片预加载
 * @param  {array} src_arrs {图片链接数组}
 * @return {array} {加载后的图片数组}
 */
export function imgPreLoad(src_arrs) {
	const imgs = [];

	src_arrs.forEach((src, index) => {
		const img = new Image();

		img.src = src;

		imgs[index] = img;
	});

	return imgs;
}

/**
 * 为dom增加class
 * @param  {string} class_name {需要增加class的dom的类名}
 */
export function domAddClass(class_name, add_class) {
	const main_div = document.querySelector(class_name);

	if (!main_div) return;

	if (main_div.className.indexOf(add_class) === -1)main_div.className += ` ${add_class}`;
}

/**
 * 获取html根元素的font-size
 * @return {string} {返回的html根元素的font-size}
 */
export function getHtmlFontSize() {
	const html = document.querySelector('html');

	return html.style.fontSize.replace(/px/, '');
}

/**
 * 获取元素的margin、padding等属性
 * @param  {string} dom  {需要获取的元素的选择器名称}
 * @param  {string} attr {需要获取的属性名}
 * @return {string} {获取到的属性值}
 */
export function getComputedStyle(dom, attr) {
	const obj = document.querySelector(dom);

	if (obj.currentStyle) {
		return obj.currentStyle[attr];
	}

	return document.defaultView.getComputedStyle(obj, null)[attr].replace(/px/, '');
}

/**
 * 字符串转日期或返回Null
 * @param date_str
 * @return {Date|null}
 */
export function toDate(date_str) {
  if (! date_str) {
    return null;
  }

  if (date_str instanceof Date) {
    return validDateOrNull(date_str)
  }

  if (typeof date_str !== 'string') {
    return null;
  }

  const date = new Date(date_str.replace(' ', 'T'));

  return validDateOrNull(date);
}

/**
 * 判断日期正常或返回空
 *
 * @param {Date} date
 * @return {Date|null}
 */
export function validDateOrNull(date) {
  if (isNaN(date.getDate())) {
    return null;
  } else {
    return date
  }
}